import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:proj_sfv/http/requests.dart';
import 'package:proj_sfv/store/MyArguments.dart';
import 'package:provider/provider.dart';
import 'package:tencent_cloud_chat_uikit/business_logic/view_models/tui_friendship_view_model.dart';
import 'package:tencent_cloud_chat_uikit/data_services/services_locatar.dart';
import 'package:tencent_cloud_chat_uikit/tencent_cloud_chat_uikit.dart';
import 'package:tencent_cloud_chat_uikit/ui/widgets/avatar.dart';

class FriendRequestsPage extends StatelessWidget {
  /// @title:"itemBuilder为用于自定义构建新的联系人用户模块的构造器."
  /// @title:"代码示例为使用自定义itemBuilder展示新的联系人用户信息."
  /// @picture:"https://qcloudimg.tencent-cloud.cn/raw/079930644efdbfc2ac628420afab6bd9.jpg"
  @override
  Widget build(BuildContext context) {
    // final theme = Provider.of(context).theme;
    TUIFriendShipViewModel model = serviceLocator<TUIFriendShipViewModel>();
    _getShowName(V2TimFriendApplication item) {
      final nickName = item.nickname ?? "";
      final userID = item.userID;
      return nickName != "" ? nickName : userID;
    }
    // 获得参数
    var args = ModalRoute.of(context).settings.arguments as MyArguments;

    Widget _itemBuilder(
        BuildContext context, V2TimFriendApplication applicationInfo) {
      final theme = Provider.of<TUIThemeViewModel>(context).theme;
      final showName = _getShowName(applicationInfo);
      final faceUrl = applicationInfo.faceUrl ?? "";
      return Container(
        padding: const EdgeInsets.only(top: 10, left: 16),
        child: Row(
          children: [
            Container(
              padding: const EdgeInsets.only(bottom: 12),
              margin: const EdgeInsets.only(right: 12),
              child: SizedBox(
                height: 40,
                width: 40,
                child: Avatar(faceUrl: faceUrl, showName: showName),
              ),
            ),
            Expanded(
                child: Container(
                  padding: const EdgeInsets.only(top: 10, bottom: 20),
                  decoration: BoxDecoration(
                      border: Border(
                          bottom: BorderSide(
                              color: theme.weakDividerColor ??
                                  CommonColor.weakDividerColor))),
                  child: Row(
                    mainAxisAlignment: MainAxisAlignment.spaceBetween,
                    children: [
                      Text(
                        showName,
                        style: const TextStyle(color: Colors.black, fontSize: 18),
                      ),
                      Expanded(child: Container()),
                      Container(
                        margin: const EdgeInsets.only(right: 8),
                        child: InkWell(
                          child: Container(
                            padding: const EdgeInsets.symmetric(
                                horizontal: 14, vertical: 6),
                            decoration: BoxDecoration(
                                borderRadius: BorderRadius.circular(8),
                                color: theme.primaryColor,
                                border: Border.all(
                                    width: 1,
                                    color: theme.weakTextColor ??
                                        CommonColor.weakTextColor)),
                            child: Text(
                              TIM_t("同意"),
                              style: const TextStyle(
                                color: Colors.white,
                              ),
                            ),
                          ),
                          onTap: () async {
                            // 接受好友请求
                            String text = await addFriendRequest(context, args.id, args.token,
                                int.parse(applicationInfo.userID), meg: FRIEND_RELATION_MEG.ACP);
                            if (text != null) {
                              await model.acceptFriendApplication(
                                applicationInfo.userID,
                                applicationInfo.type,
                              );
                              model.loadData();
                            }
                          },
                        ),
                      ),
                      Container(
                          margin: const EdgeInsets.only(right: 8),
                          child: InkWell(
                            child: Container(
                              decoration: BoxDecoration(
                                  borderRadius: BorderRadius.circular(8),
                                  color: Colors.white,
                                  border: Border.all(
                                      width: 1,
                                      color: theme.weakTextColor ??
                                          CommonColor.weakTextColor)),
                              padding: const EdgeInsets.symmetric(
                                  horizontal: 14, vertical: 6),
                              child: Text(
                                TIM_t("拒绝"),
                                style: TextStyle(
                                  color: theme.primaryColor,
                                ),
                              ),
                            ),
                            onTap: () async {
                              // 拒绝好友请求
                              String text = await addFriendRequest(context, args.id, args.token,
                                  int.parse(applicationInfo.userID), meg: FRIEND_RELATION_MEG.REJ);
                              if (text != null) {
                                await model.refuseFriendApplication(
                                  applicationInfo.userID,
                                  applicationInfo.type,
                                );
                                model.loadData();
                              }
                            },
                          ))
                    ],
                  ),
                ))
          ],
        ),
      );
    }

    return Scaffold(
      appBar: AppBar(
          title: Text(
            "新的联系人",
            style: const TextStyle(color: Colors.white, fontSize: 17),
          ),
          shadowColor: Colors.white,
          flexibleSpace: Container(
            decoration: BoxDecoration(
              gradient: LinearGradient(colors: [
                CommonColor.lightPrimaryColor,
                CommonColor.primaryColor
              ]),
            ),
          ),
          iconTheme: const IconThemeData(
            color: Colors.white,
          )),
      body: TIMUIKitNewContact(
        itemBuilder: _itemBuilder,
        emptyBuilder: (c) {
          return Center(
            child: Text("暂无新联系人"),
          );
        },
      ),
    );
  }
}